<?php
/*
 +----------------------------------------------------------------------
 + Title        : CrossDomain 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/8/8 19:41
 + Last-time    : 2019/8/8 19:41+ 86187
 + Desc         : CrossDomain
 +----------------------------------------------------------------------
*/


namespace app\http\middleware;


class CrossDomain {
    public function handle($request, \Closure $next) {

        $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';

        $allow_origin = [
            'http://localhost:8080',
            'http://www.client2.com'
        ];
        if (in_array($origin, $allow_origin)) {
            header('Access-Control-Allow-Origin:' . $origin);
            header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
            header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
        }

        //允许所有ip访问，设置请求类型

        return $next($request);
    }


}